package com.wasu.sdk_ott.service

import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.wasu.constant.TAG
import com.wasu.entity.live.WasuBouChInfor
import com.wasu.entity.live.WasuBouInfor
import com.wasu.entity.live.WasuChInfor
import com.wasu.sdk_ott.http.live.IPLIVE_CACHE_CHANNEL_SORT_KYE
import com.wasu.sdk_ott.http.live.loadCache
import com.wasu.sdk_ott.http.live.saveCache
import java.util.Collections

object LiveSortHelper {

    /**
     * 分组频道列表，排序
     */
    fun sortChannelGroup(wasuBouChInfor: WasuBouChInfor): WasuBouChInfor {
        var sortChannelGroup: WasuBouChInfor = wasuBouChInfor
        val defaultKey = IPLIVE_CACHE_CHANNEL_SORT_KYE
        val gson = Gson()

        val failure = { throwable: Throwable ->
            Log.v(TAG, "sortChannelGroup loadCache failure")
            val wasuBouInforList = wasuBouChInfor.getbList()
            val allChInforList: MutableList<WasuChInfor> = ArrayList()
            val allChannelIds: MutableList<String> = ArrayList()
            // >=0 判断是否包含全部/设置/收藏分组
            // >=0 判断是否包含全部/设置/收藏分组
            var allBouIndex = -1
            var setBouIndex = -1
            var collBouIndex = -1

            for (i in wasuBouInforList.indices) {
                val wasuBouInfor = wasuBouInforList[i]
                // 1 判断如果没有全部分组，终端手动添加，频道为其他分组所有频道
                // 2 判断如果有全部分组，且不是列表最后一个，则将全部挪到最后一个索引位置
                if (!TextUtils.isEmpty(wasuBouInfor.getbName()) && "全部" == wasuBouInfor.getbName()
                        .trim { it <= ' ' }
                ) {
                    allBouIndex = i
                }
                val wasuChannelList = wasuBouInfor.chList
                for (channel in wasuChannelList) {
                    if (allChannelIds.contains(channel.chId)) {
                        continue
                    }
                    allChInforList.add(channel)
                    allChannelIds.add(channel.chId)
                }
                // 修改设置和收藏分组名称，同步现网
                if (!TextUtils.isEmpty(wasuBouInfor.getbName()) && "收藏" == wasuBouInfor.getbName()
                        .trim { it <= ' ' }
                ) {
                    collBouIndex = i
                }
                if (!TextUtils.isEmpty(wasuBouInfor.getbName()) && "设置" == wasuBouInfor.getbName()
                        .trim { it <= ' ' }
                ) {
                    setBouIndex = i
                }
                // 对所有频道排序
                if (wasuBouInfor.chList != null && wasuBouInfor.chList.size > 0) {
                    Collections.sort(
                        wasuBouInfor.chList
                    ) { o1: WasuChInfor?, o2: WasuChInfor? ->
                        if (o1 == null || o2 == null) {
                            return@sort 0
                        }
                        Integer.compare(o1.chNo, o2.chNo)
                    }
                }
            }

            Log.v(
                TAG,
                "queryChannelGroup, allBouIndex:$allBouIndex; setBouIndex:$setBouIndex; collBouIndex:$collBouIndex"
            )
            Log.v(
                TAG,
                "queryChannelGroup, 全部频道id(用来去重):" + allChannelIds.size + "; 全部频道(在没有全部分组的时候添加):" + allChInforList.size
            )
            // 修改全部分组的位置，IPTV模式，allBouIndex 会一直小于0
            // 修改全部分组的位置，IPTV模式，allBouIndex 会一直小于0
            if (allBouIndex >= 0 && allBouIndex < wasuBouInforList.size) {
                // 如果存在的全部分组不在列表第一个，则手动修改到第一个，如果已经是第一个，则不做处理
                if (allBouIndex != 0) {
                    val wasuBouInfor = wasuBouInforList[allBouIndex]
                    wasuBouInforList.removeAt(allBouIndex)
                    wasuBouInforList.add(0, wasuBouInfor)
                }
            } else if (allBouIndex < 0 && allChannelIds.size > 0) {
                Collections.sort(allChInforList) { o1: WasuChInfor?, o2: WasuChInfor? ->
                    if (o1 == null || o2 == null) {
                        return@sort 0
                    }
                    Integer.compare(o1.chNo, o2.chNo)
                }
                val wasuBouInfor = WasuBouInfor()
                wasuBouInfor.setbName("全部")
                wasuBouInfor.setbEnName("All")
                wasuBouInfor.setbId("1000")
                wasuBouInfor.setbNumbers(allChInforList.size)
                wasuBouInfor.firstChId = allChInforList[0].chId
                wasuBouInfor.chList = allChInforList
                wasuBouInforList.add(0, wasuBouInfor)
            }

            // 表示原分组中没有收藏分组，需要手动add

            // 表示原分组中没有收藏分组，需要手动add
            if (collBouIndex < 0) {
                val wasuBouInfor = WasuBouInfor()
                wasuBouInfor.setbName("收藏")
                wasuBouInfor.setbId("1010")
                wasuBouInforList.add(wasuBouInfor)
            }
            // 表示原分组中没有设置分组，需要手动add
            // 表示原分组中没有设置分组，需要手动add
            if (setBouIndex < 0) {
                val wasuBouInfor = WasuBouInfor()
                wasuBouInfor.setbName("设置")
                wasuBouInfor.setbId("1100")
                wasuBouInforList.add(wasuBouInfor)
            }
            saveCache(defaultKey = defaultKey, content = gson.toJson(wasuBouChInfor))
        }

        loadCache(defaultKey = defaultKey, success = {
            Log.v(TAG, "sortChannelGroup loadCache  success")
            try {
                val cacheBouChInfor = gson.fromJson(it, WasuBouChInfor::class.java)
                if (cacheBouChInfor.version == wasuBouChInfor.version) {
                    // 缓存版本号相同，直接返回缓存数据
                    sortChannelGroup = cacheBouChInfor
                } else {
                    // 缓存版本号不同，重新排序,并更新排序后的缓存数据
                    failure(Exception("缓存版本号不同"))
                }
            } catch (e: Exception) {
                e.printStackTrace()
                failure(e)
            }

        }, failure = failure)
        return sortChannelGroup
    }
}